<?php

class Forum
{
	private $_mysql = null;
	
	private $_forumId = 0;
	private $_userId = 0;
	
	public function Forum($forumId, $userId)
	{
		$this->_mysql = Mysql::getInstance();
		
		$this->_forumId = $forumId;
		$this->_userId = $userId;
	}
	
	public function deletePost($threadId, $postId)
	{
		$sql = <<<END
			UPDATE forum_posts 
			SET removed = 1
			WHERE id = {$postId}
END;
		$this->_mysql->query($sql);
		
		// Thread last post
		$post = $this->_findThreadLastPost($threadId);
		if($post['id'] == $postId)
		{
			$this->_threadChangeLastPost($threadId, $post['id'], $post['user'], $post['time']);
		}
		
		// Forum last post
		$post = $this->_findForumLastPost();
		if($post['id'] == $postId)
		{
			$this->_forumChangeLastPost($post['id'], $post['user'], $post['time']);
		}
		
		$this->_decreaseThreadPosts();
		$this->_decreaseUserPosts();
	}
	
	public function deleteThread($postId)
	{
		$sql = <<<END
			UPDATE forum_threads 
			SET removed = 1
			WHERE post_id = {$postId}
END;
		$this->_mysql->query($sql);
		
		$this->deactivateThread($postId);
	}
	
	public function insertPost($threadId, $text, $active = 1)
	{
		$time = time();
		
		$sql = <<<END
			INSERT INTO forum_posts 
			SET thread_id = {$threadId},
			text = '{$text}',
			created_by_user = {$this->_userId},
			created_by_ip = '{$_SERVER['REMOTE_ADDR']}',
			created_time = {$time}
END;
		$this->_mysql->query($sql);
		$postId = $this->_mysql->getLastInsertId();
		
		$this->_threadChangeLastPost($threadId, $postId, $this->_userId, $time);
		if($active)
		{
			$this->_forumChangeLastPost($postId, $this->_userId, $time);
		}
		
		$this->_increaseThreadPosts($threadId);
		$this->_increaseUserPosts();
		
		return $postId;
	}
	
	/**
	 * Insert a new thread.
	 *  title - The threads title.
	 *  text - The posts text.
	 *  active - If the thread should be active.
	 */
	public function insertThread($title, $text, $active = 1)
	{
		$sql = <<<END
			INSERT INTO forum_threads 
			SET forum_id = {$this->_forumId},
			title = '{$title}',
			active = {$active}
END;
		$this->_mysql->query($sql);
		$threadId = $this->_mysql->getLastInsertId();
		
		$postId = $this->insertPost($threadId, $text, $active);
		
		$sql = <<<END
			UPDATE forum_threads 
			SET post_id = {$postId}
			WHERE id = {$threadId}
END;
		$this->_mysql->query($sql);
		
		if($active)
		{
			$this->_increaseForumThreads();
			$this->_increaseUserThreads();
		}
		
		return $threadId;
	}
	
	public function updatePost($postId, $text)
	{
		$sql = <<<END
			UPDATE forum_posts 
			SET text = '{$text}',
			updated_by_ip = '{$_SERVER['REMOTE_ADDR']}',
			updated_time = UNIX_TIMESTAMP()
			WHERE id = {$postId}
END;
		$this->_mysql->query($sql);
	}
	
	public function updateThread($threadId, $title, $active = 1)
	{
		$sql = <<<END
			UPDATE forum_threads 
			SET title = '{$title}',
			active = {$active}
			WHERE id = {$threadId}
END;
		$this->_mysql->query($sql);
	}
	
	public function activateThread($postId)
	{
		$sql = <<<END
			SELECT last_post_user AS user, last_post_time AS time
			FROM forum_threads 
			WHERE post_id = {$postId}
END;
		$query = $this->_mysql->query($sql);
		$thread = mysql_fetch_assoc($query);
		
		$this->_forumChangeLastPost($postId, $thread['user'], $thread['time']);
		
		$this->_increaseForumThreads();
		$this->_increaseUserThreads();
	}
	
	public function deactivateThread($postId)
	{
		$sql = <<<END
			SELECT last_post_id AS id, last_post_user AS user, last_post_time AS time 
			FROM forums 
			WHERE id = {$this->_forumId}
END;
		$query = $this->_mysql->query($sql);
		$post = mysql_fetch_assoc($query);
		
		if($post['id'] == $postId)
		{
			$thread = $this->_findForumLastPost();
			$this->_forumChangeLastPost($thread['id'], $thread['user'], $thread['time']);
		}
		
		$this->_decreaseForumThreads();
		$this->_decreaseUserThreads();
	}
	
	
	/**
	 * Change last post functions.
	 */
	
	public function _forumChangeLastPost($id, $user, $time)
	{
		$sql = <<<END
			UPDATE forums 
			SET last_post_id = {$id},
			last_post_user = {$user},
			last_post_time = {$time}
			WHERE id = {$this->_forumId}
END;
		$this->_mysql->query($sql);
	}
	
	public function _threadChangeLastPost($threadId, $id, $user, $time)
	{
		$sql = <<<END
			UPDATE forum_threads 
			SET last_post_id = {$id},
			last_post_user = {$user},
			last_post_time = {$time}
			WHERE id = {$threadId}
END;
		$this->_mysql->query($sql);
	}
	
	
	/**
	 * Find help functions.
	 */
	
	public function _findForumLastPost()
	{
		// Find a new last post.
		$sql = <<<END
			SELECT t.last_post_id AS id, t.last_post_user AS user, t.last_post_time AS time
			FROM forum_threads AS t
				JOIN forum_posts AS p ON t.post_id = p.id
			WHERE t.forum_id = {$this->_forumId}
				AND t.active = 1
				AND p.removed = 0
			ORDER BY t.last_post_time DESC
			LIMIT 1
END;
		$query = $this->_mysql->query($sql);
		if(mysql_num_rows($query))
		{
			$post = mysql_fetch_assoc($query);
		}
		else
		{
			$post['id'] = 0;
			$post['user'] = 0;
			$post['time'] = 0;
		}

		return $post;
	}
	
	public function _findThreadLastPost($threadId)
	{
		// Find a new last post.
		$sql = <<<END
			SELECT id, created_by_user AS user, created_time AS time 
			FROM forum_posts  
			WHERE thread_id = {$threadId}
				AND removed = 0
			ORDER BY t.last_post_time DESC
			LIMIT 1
END;
		$query = $this->_mysql->query($sql);
		if(mysql_num_rows($query))
		{
			$post = mysql_fetch_assoc($query);
		}
		else
		{
			$post['id'] = 0;
			$post['user'] = 0;
			$post['time'] = 0;
		}
		
		return $post;
	}
	
	
	/**
	 * Private help functions.
	 */
	
	private function _decreaseForumThreads()
	{
		$sql = <<<END
			UPDATE forums 
			SET threads = threads - 1
			WHERE id = {$this->_forumId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _decreaseThreadPosts($threadId)
	{
		$sql = <<<END
			UPDATE forum_threads 
			SET posts = posts - 1
			WHERE id = {$threadId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _decreaseUserPosts()
	{
		$sql = <<<END
			UPDATE user_info 
			SET forum_posts = forum_posts - 1 
			WHERE user_id = {$this->_userId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _decreaseUserThreads()
	{
		$sql = <<<END
			UPDATE user_info 
			SET forum_threads = forum_threads - 1 
			WHERE user_id = {$this->_userId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _increaseForumThreads()
	{
		$sql = <<<END
			UPDATE forums 
			SET threads = threads + 1
			WHERE id = {$this->_forumId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _increaseThreadPosts($threadId)
	{
		$sql = <<<END
			UPDATE forum_threads 
			SET posts = posts + 1
			WHERE id = {$threadId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _increaseUserPosts()
	{
		$sql = <<<END
			UPDATE user_info 
			SET forum_posts = forum_posts + 1 
			WHERE user_id = {$this->_userId}
END;
		$this->_mysql->query($sql);
	}
	
	private function _increaseUserThreads()
	{
		$sql = <<<END
			UPDATE user_info 
			SET forum_threads = forum_threads + 1 
			WHERE user_id = {$this->_userId}
END;
		$this->_mysql->query($sql);
	}
}

?>